package com.wyp168.gulimall.ware.service.impl;

import com.wyp168.common.to.SkuHasStockVO;
import com.wyp168.common.utils.R;
import com.wyp168.gulimall.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.Query;

import com.wyp168.gulimall.ware.dao.WareSkuDao;
import com.wyp168.gulimall.ware.entity.WareSkuEntity;
import com.wyp168.gulimall.ware.service.WareSkuService;
import org.springframework.util.CollectionUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    public static final Boolean NO_STOCK = false;

    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuHasStockVO> getSkuHasStock(List<Long> skuIds) {
        if (CollectionUtils.isEmpty(skuIds)) {
            return new ArrayList<>();
        }
        List<SkuHasStockVO> vos = new ArrayList<>();
        skuIds.forEach(skuId -> {
            Long stackNum = baseMapper.selectStackNumBySkuId(skuId);
            SkuHasStockVO skuHasStockVO = new SkuHasStockVO();
            skuHasStockVO.setSkuId(skuId);
            if (Objects.isNull(stackNum)) {
                skuHasStockVO.setHasStock(NO_STOCK);
            } else {
                skuHasStockVO.setHasStock(stackNum > 0);
            }
            vos.add(skuHasStockVO);
        });
        return vos;
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //判断是否有库存记录
        Integer counts = wareSkuDao.selectCount(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (counts > 0) {    //有对应库存，则走更新逻辑
            wareSkuDao.addStock(skuId, wareId, skuNum);
        } else {    //没对应库存，则走新增逻辑
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            //调用远程服务，获取sku名称
            //当远程调用失败，阻止事务回滚
            //方式1.使用try...catch语句块来捕获异常
            //TODO 方式二？
            try {
                R info = productFeignService.info(skuId);
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");

                if (info.getCode() == 0) {
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            wareSkuDao.insert(wareSkuEntity);
        }
    }

}